package com.yingside.io.file;

import java.io.File;
import java.io.FileFilter;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.Arrays;

public class TestFile {

    public void test(){
        //windows绝对路径  盘符:/文件夹名字/xxxx
        File file = new File("/Users/yingside/Desktop/io/file.txt");
        boolean exists = file.exists();
        //System.out.println(exists);
        //如果没有就创建
        if (!exists){
            //创建单个文件夹
//            boolean mkdir = file.mkdir();
//            System.out.println("mkdir = " + mkdir);
            //创建多个文件夹
//            boolean mkdirs = file.mkdirs();
//            System.out.println("mkdirs = " + mkdirs);

            //创建文件
            try {
                boolean newFile = file.createNewFile();
                System.out.println("newFile = " + newFile);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        else{
            //如果文件或者文件夹已经存在，可以读取相关信息
            //获取文件名字
//            String name = file.getName();
//            System.out.println("name = " + name);

            //获取文件长度
//            long length = file.length();
//            System.out.println("length = " + length);

            //获取文件读取或者写的权限
//            boolean b = file.canWrite();
//            System.out.println("b = " + b);

            //文件或者文件夹的删除
            //如果是一个空的文件夹才能直接删除，不能删除有内容的文件夹
//            boolean delete = file.delete();
//            System.out.println("delete = " + delete);

            //获取文件的绝对路径
//            String absolutePath = file.getAbsolutePath();
//            System.out.println("absolutePath = " + absolutePath);

            //得到文件的父文件夹名字
//            String parent = file.getParent();
//            System.out.println("parent = " + parent);

            //得到父文件夹的File对象
            File parentFile = file.getParentFile();
            //判断是不是一个文件夹
//            boolean directory = parentFile.isDirectory();
//            System.out.println("directory = " + directory);


//            String[] list = parentFile.list();
//            System.out.println("Arrays.toString(list) = " + Arrays.toString(list));


            // 使用内部类过滤
//            String[] list = parentFile.list(new MyFilenameFilter2());
//            System.out.println("Arrays.toString(list) = " + Arrays.toString(list));

            // 使用匿名内部类进行过滤
            /*String[] files = parentFile.list(new FilenameFilter() {
                public boolean accept(File dir, String name) {
                    if (name.indexOf("file") != -1) {
                        return true;
                    }
                    return false;
                }
            });

            System.out.println("Arrays.toString(files) = " + Arrays.toString(files));*/


//            File[] files = parentFile.listFiles();
//
//            for (File f:files) {
//                boolean directory = f.isDirectory();
//                if(directory){
//                    boolean delete = f.delete();
//                    System.out.println("delete = " + delete);
//                }
//            }


            File[] files = parentFile.listFiles(new FileFilter() {
                public boolean accept(File file) {
                    if (file.isFile()) {
                        return true;
                    }
                    return false;
                }
            });

            for (File f:files) {
                boolean delete = f.delete();
                System.out.println("delete = " + delete);
            }

        }
    }



    public class MyFilenameFilter2 implements FilenameFilter{

        @Override
        public boolean accept(File dir, String name) {
            if(name.indexOf("New") != -1){
                return true;
            }
            return false;
        }
    }

    public static void main(String[] args) {
        TestFile testFile = new TestFile();
        testFile.test();
    }
}
